#!/bin/bash
##################################################################
## function:chean_time_cost  clean  original_timecost_log to timecost_log table
## eg：bash chean_time_cost.sh 20150505
## author: shx
## createdate: 20150608 
## version:     
################################################################## 
. /home/hadoop/shx/baseUtil.sh


if [[ ! -n "$1" ]] && [[ ! -n "$2" ]] ;then
	HISTORYDATE=`date -d '-1 hours'  +%Y%m%d`
	HOUR=`date -d '-1 hours' +%H`
else
	HISTORYDATE=$1
	HOUR=$2
fi


echo `date`"==========clean timecost_log start===============" 
/usr/local/cloud/hive/bin/hive -e  " 
use qyer;
ALTER TABLE original_timecost_log DROP PARTITION(logdate='$HISTORYDATE',loghour='$HOUR');
ALTER TABLE original_timecost_log ADD PARTITION (logdate='$HISTORYDATE' ,loghour='$HOUR') location '/flume/original/timecost/$HISTORYDATE/$HOUR';

ALTER TABLE timecost_log DROP PARTITION(logdate='$HISTORYDATE' , loghour='$HOUR');
ALTER TABLE timecost_log ADD PARTITION (logdate='$HISTORYDATE' ,loghour='$HOUR') ;


delete file /home/hadoop/shx/clean_timecost/tomaps_timecost.py;
add file /home/hadoop/shx/clean_timecost/tomaps_timecost.py;
insert overwrite table timecost_log partition (logdate = '$HISTORYDATE' ,loghour='$HOUR') 
SELECT  
  TRANSFORM (line)  
  USING 'python tomaps_timecost.py'  
  AS (
    logtime string, 
    api_name string, 
    request_id string, 
    context string,
    request_time int ,
    total_rpc_time int,
    single_rpc_time int , 
    engine_execution_time int,
    other_times string
    )  
FROM original_timecost_log 
where logdate='$HISTORYDATE' and loghour='$HOUR'
;
"

if [ $? -eq 0 ]
  then
  	echo `date`"=============== clean timecost_log is ok  end===============" 
else
  	echo `date`"================clean timecost_log is error end !================" 
  	mailSend "127 :clean timecost_log is error end !"
fi








